//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.purvar.boot.comm.util;

import com.purvar.boot.comm.annotation.FieldIgnore;
import com.purvar.boot.comm.api.vo.ColumnEntity;
import com.purvar.boot.comm.api.vo.ColumnEntityExtend;
import com.purvar.boot.comm.api.vo.TableEntity;
import com.purvar.boot.comm.constant.FieldHelpConstant;
import org.apache.commons.lang.WordUtils;
import org.springframework.core.env.Environment;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GenUtils {
    public GenUtils() {
    }

    public static TableEntity generatorCode(Environment env, Map<String, String> table, List<Map<String, Object>> columns) {
        boolean hasBigDecimal = false;
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName((String) table.get("tableName"));
        tableEntity.setComments((String) table.get("tableComment"));
        String className = tableToJava(tableEntity.getTableName());
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));
        if (columns != null) {
            List<ColumnEntityExtend> columsList = new ArrayList();
            Iterator var7 = columns.iterator();

            while (var7.hasNext()) {
                Map<String, Object> column = (Map) var7.next();
                if (column.get("columnName") != null && isBasicFiled(ObjectToString(column.get("columnName"))) && isIgnoreField(ObjectToString(column.get("columnName")), (String) table.get("tableName"))) {
                    ColumnEntityExtend columnEntity = new ColumnEntityExtend();
                    columnEntity.setColumnName(ObjectToString(column.get("columnName")));
                    columnEntity.setDataType(ObjectToString(column.get("dataType")));
                    columnEntity.setComments(ObjectToString(column.get("columnComment")));
                    columnEntity.setExtra(ObjectToString(column.get("extra")));
                    String attrName = columnToJava(columnEntity.getColumnName());
                    columnEntity.setAttrName(attrName);
                    columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
                    String attrType = env.getProperty(columnEntity.getDataType(), "unknowType");
                    columnEntity.setAttrType(attrType);
                    if (!hasBigDecimal && attrType.equals("BigDecimal")) {
                        hasBigDecimal = true;
                    }

                    if ("PRI".equalsIgnoreCase(ObjectToString(column.get("columnKey"))) && tableEntity.getPk() == null) {
                        tableEntity.setPk(columnEntity);
                    }

                    columsList.add(columnEntity);
                }
            }

            tableEntity.setColumns(columsList);
        }

        if (tableEntity.getPk() == null && tableEntity.getColumns() != null) {
            tableEntity.setPk((ColumnEntity) tableEntity.getColumns().get(0));
        }

        return tableEntity;
    }

    public static String ObjectToString(Object o) {
        return o != null ? o.toString() : "";
    }

    public static TableEntity generatorDataBaseCode(Environment env, Map<String, String> table, List<Map<String, Object>> columns) {
        boolean hasBigDecimal = false;
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName((String) table.get("tablename"));
        tableEntity.setComments((String) table.get("tablecomment"));
        String className = tableToJava(tableEntity.getTableName());
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));
        if (columns != null) {
            List<ColumnEntityExtend> columsList = new ArrayList();
            Iterator var7 = columns.iterator();

            while (var7.hasNext()) {
                Map<String, Object> column = (Map) var7.next();
                if (column.get("columnname") != null) {
                    ColumnEntityExtend columnEntity = new ColumnEntityExtend();
                    columnEntity.setColumnName(ObjectToString(column.get("columnname")));
                    columnEntity.setDataType(ObjectToString(column.get("datatype")));
                    columnEntity.setComments(ObjectToString(column.get("columncomment")));
                    columnEntity.setExtra(ObjectToString(column.get("extra")));

                    try {
                        columnEntity.setMaximumLength(Integer.parseInt(ObjectToString(column.get("maximum_length"))));
                    } catch (Exception var12) {
                        columnEntity.setMaximumLength(0);
                    }

                    String attrName = columnToJava(columnEntity.getColumnName());
                    columnEntity.setAttrName(attrName);
                    columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
                    String attrType = env.getProperty(columnEntity.getDataType(), "unknowType");
                    columnEntity.setAttrType(attrType);
                    if (!hasBigDecimal && attrType.equals("BigDecimal")) {
                        hasBigDecimal = true;
                    }

                    if ("PRI".equalsIgnoreCase(ObjectToString(column.get("column_key"))) && tableEntity.getPk() == null) {
                        columnEntity.setMainId("true");
                        tableEntity.setPk(columnEntity);
                    }

                    columsList.add(columnEntity);
                }
            }

            tableEntity.setColumns(columsList);
        }

        if (tableEntity.getPk() == null && tableEntity.getColumns() != null) {
            tableEntity.setPk((ColumnEntity) tableEntity.getColumns().get(0));
        }

        return tableEntity;
    }

    public static boolean isBasicFiled(String Filed) {
        if ("del_Flag".equalsIgnoreCase(Filed)) {
            return false;
        } else if ("id".equalsIgnoreCase(Filed)) {
            return false;
        } else if ("update_Time".equalsIgnoreCase(Filed)) {
            return false;
        } else if ("update_By".equalsIgnoreCase(Filed)) {
            return false;
        } else if ("create_Time".equalsIgnoreCase(Filed)) {
            return false;
        } else if ("create_By".equalsIgnoreCase(Filed)) {
            return false;
        } else {
            return !"creater_org_path".equalsIgnoreCase(Filed);
        }
    }

    public static boolean isIgnoreField(String field, String tableName) {
        String classPackPach = (String) FieldHelpConstant.fieldMap.get(tableName);
        if (StringUtils.isEmpty(classPackPach)) {
            return true;
        } else {
            Class aClass;
            try {
                aClass = Class.forName(classPackPach);
            } catch (ClassNotFoundException var7) {
                return true;
            }

            Field fields;
            try {
                fields = aClass.getDeclaredField(field);
            } catch (NoSuchFieldException var6) {
                return true;
            }

            Annotation annotation = fields.getAnnotation(FieldIgnore.class);
            return annotation == null;
        }
    }

    public static String columnToJava(String columnName) {
        String word = WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
        return toLowerCaseFirstOne(word);
    }

    public static String toLowerCaseFirstOne(String s) {
        return Character.isLowerCase(s.charAt(0)) ? s : Character.toLowerCase(s.charAt(0)) + s.substring(1);
    }

    public static String toUpCaseFirstOne(String s) {
        return Character.isUpperCase(s.charAt(0)) ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    public static String tableToJava(String tableName) {
        String word = WordUtils.capitalizeFully(tableName, new char[]{'_'}).replace("_", "");
        return word;
    }
}
